/*
 * Copyright (C) 2018 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xdlinkdemo.entity;

import com.xuexiang.xdlink.component.message.IDataInfo;
import com.xuexiang.xutil.common.StringUtils;

/**
 * api消息实体
 *
 * @author xuexiang
 * @since 2018/9/3 下午8:42
 */
public class ApiEntity<T> implements IDataInfo<T> {

    public static final String KEY_CODE = "Code";
    public static final String KEY_MSG = "Msg";
    public static final String KEY_TYPE = "Type";
    public static final String KEY_DATA = "Data";

    /**
     * 结果码
     */
    private int Code = 0;

    /**
     * 错误原因
     */
    private String Msg = "";

    /**
     * API类型
     */
    private String Type;

    /**
     * 数据
     */
    private T Data;

    public ApiEntity(String type) {
        Type = type;
    }

    public int getCode() {
        return Code;
    }

    public ApiEntity<T> setCode(int code) {
        Code = code;
        return this;
    }

    public String getMsg() {
        return Msg;
    }

    public ApiEntity<T> setMsg(String msg) {
        Msg = msg;
        return this;
    }

    public String getType() {
        return Type;
    }

    public ApiEntity<T> setType(String type) {
        Type = type;
        return this;
    }

    public T getData() {
        return Data;
    }

    public ApiEntity<T> setData(T data) {
        Data = data;
        return this;
    }

    public boolean isSuccess() {
        return Code == 0 && !StringUtils.isEmpty(Type);
    }

    @Override
    public T getDataInfo() {
        return getData();
    }
}
